package com.xm.export.excel;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.poi.ss.usermodel.Workbook;

import java.io.File;
import java.io.FileOutputStream;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.List;

public class SimpleTest {
    public static void main(String[] args) throws Exception {
        //数据
        File dataFile = new File("D:\\myproject\\export-plus\\src\\main\\java\\com\\xm\\export\\excel\\data.json");
        List<String> strings = Files.readAllLines(dataFile.toPath());
        StringBuilder sb = new StringBuilder();
        strings.forEach(sb::append);
        JSONArray jsonArray = JSONArray.parseArray(sb.toString());
        List<JSONObject> dataList = new ArrayList<>();
        for (int i = 0; i < jsonArray.size(); i++) {
            dataList.add(jsonArray.getJSONObject(i));
        }
        //配置
        File configFile = new File("D:\\myproject\\export-plus\\src\\main\\java\\com\\xm\\export\\excel\\config.json");
        List<String> strings1 = Files.readAllLines(configFile.toPath());
        StringBuilder sb1 = new StringBuilder();
        strings1.forEach(sb1::append);
        Exporter exporter = new Exporter(sb1.toString());
        try (Workbook workbook = exporter.export(dataList)) {
            File file = new File("test." + exporter.getExcelType());
            file.createNewFile();
            try (FileOutputStream fos = new FileOutputStream(file)) {
                workbook.write(fos);
            }
        }
    }
}
